extends Camera2D

var speed_scale = 0.1
var speed_move = 5
var zoom_min = 0.45
var zoom_common = 1.5
var zoom_max = 1.5			# 越大说明距离地图越近
var is_zoom_max = false

func _ready():
	zoom = Vector2(zoom_min,zoom_min)
	await get_tree().create_timer(5).timeout
	var me_player = ObjectManager.gameManager.me_player as Player
	if !me_player || !me_player.alive:
		return
	var pos = ObjectManager.gameManager.me_player.small.position
	lensControl(pos,Vector2(zoom_common,zoom_common))
	

func _process(_delta):
	if !GameUtils.getIsCanControlCamera():
		return
	var scalee:float = 0
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_AMPLIFY) == 1:
		scalee = speed_scale
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_REDUCE) == 1:
		scalee = -speed_scale
	if zoom.x+scalee > zoom_max:
		zoom = Vector2(zoom_max,zoom_max)
	elif zoom.x+scalee < zoom_min:
		zoom = Vector2(zoom_min,zoom_min)
	else :
		zoom += Vector2(scalee, scalee)
	
	var move_x:float = 0.0
	var move_y:float = 0.0
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_LEFT) == 1:
		move_x = -speed_move
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_RIGHT) == 1:
		move_x = speed_move
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_UP) == 1:
		move_y = -speed_move
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_DOWM) == 1:
		move_y = speed_move
	if move_x!=0 && move_y!=0:
		move_x /= 1.414
		move_y /= 1.414
	offset += Vector2(move_x,move_y)
	# 点击空格
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_CENTER):
		quickControl(Vector2(0,0))
		zoom = Vector2(zoom_min,zoom_min)
	is_zoom_max = zoom.x == zoom_max



var last_position = Vector2(0,0)
func _unhandled_input(event):
	if !GameUtils.getIsCanControlCamera():
		return
	if event is InputEventMouseMotion:
		if event.pressure == 1:
			offset += (last_position - event.position)*(Vector2(1,1)/zoom)
			last_position = event.position
	if event is InputEventMouseButton:
		var scalee:float = 0
		if event.button_index == MouseButton.MOUSE_BUTTON_WHEEL_UP:
			scalee = speed_scale
		if event.button_index == MouseButton.MOUSE_BUTTON_WHEEL_DOWN:
			scalee = -speed_scale
		if zoom.x+scalee>zoom_max:
			zoom = Vector2(zoom_max,zoom_max)
		elif zoom.x+scalee<zoom_min:
			zoom = Vector2(zoom_min,zoom_min)
		else :
			zoom += Vector2(scalee,scalee)
		if event.button_index == MouseButton.MOUSE_BUTTON_LEFT:
			last_position = event.position


# 点击point拉近摄像机
var tween:Tween
var time_move:float = 0.3		#镜头移动的时间
var time_zoomIn:float = 0.3		#镜头拉近的时间
func lensControl(pos:Vector2,zo:Vector2):
	if tween:
		tween.kill()
	tween = create_tween()
	var time = 0
	if is_zoom_max:
		time = time_move
	else:
		time = time_zoomIn
	tween.tween_property(self, "offset", pos, time)
	tween.parallel().tween_property(self, "zoom", zo, time)

# 快速设置,鼠标右键点击point、按下空格时会调用
func quickControl(pos:Vector2):
	self.offset = pos
	self.zoom = Vector2(zoom_max,zoom_max)

# 棋子行动时摄像机拉远一点
func zoomOut(zoomm:float):
	var point = ObjectManager.gameManager.operationManager.selectedPoint as Point
	lensControl(point.position,Vector2(zoomm,zoomm))

# UI按钮 缩放
func scaleDown():
	if tween:
		tween.kill()
	tween = create_tween()
	tween.tween_property(self, "zoom", Vector2(zoom_min,zoom_min), time_zoomIn)

# UI按钮 放大
func scaleUp():
	if tween:
		tween.kill()
	tween = create_tween()
	tween.tween_property(self, "zoom", Vector2(zoom_max,zoom_max), time_zoomIn)
